package view;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;

import controller.FileDoc;
import controller.SupprimAction;
import model.Carnet;
import model.Contact;
/**
 * This class is a kind of formulary, we only add a supprim button on the frame
 * Then we inialize the text of each JTextField with the information of the contact that the user wants to modify
 * @author Estelle
 *
 */
public class Modify extends Formulaire{
		private String name;
		
//--------------------------------------------------------------------------------------------------------------------------------------------------------
		
		/**
		 * the constructor needs the list of contacts to take the value selected by the user
		 * to set the JTextFields
		 * @param list
		 * @param window
		 */
		public Modify(JList<String> list, JFrame window){
			super(window);
			name = list.getSelectedValue().toString();
			Contact modifyContact = new Contact();
			
			//--------------------------------------------------------------------------------------------------------------------------------------------------------
			/**
			 * We search and get the contact selected by the user
			 */
			FileDoc file = new FileDoc();
			Carnet carnet = new Carnet("");
			carnet.copy(file.recupCarnet("Estelle"));
			if(!name.isEmpty()){
				modifyContact.copyContact(carnet.searchContact(name));
				if(modifyContact.emptyContact()) System.out.println("ERROR l31 in view.Modify : contact is empty/ not found");
			}
			else System.out.println("ERROR l32 in view.Modify : user doesn't select a name in the list");
			//--------------------------------------------------------------------------------------------------------------------------------------------------------
			/**
			 * we initialize the supprim button
			 */
			JButton supprim = new JButton("supprim");
			supprim.setBounds(50, 60, 80, 30);
	        
			supprim.setToolTipText("To add new contact");
			supprim.addActionListener(new SupprimAction(super.getForm(),name, window));
			
			//--------------------------------------------------------------------------------------------------------------------------------------------------------
			/**
			 * we set the text of each jtexfield with the information of the contact selected
			 */
		    super.getB1().getFirstName().setText(modifyContact.getFirstName());
			super.getB1().getLastName().setText(modifyContact.getLastName());
			super.getB1().getHomeAddress().setText(modifyContact.getHomeAddress());
			super.getB1().getNumberPerso().setText(modifyContact.getPersonalPhone());
			super.getB1().getCompanyAddress().setText(modifyContact.getCompanyAddress());
			super.getB1().getHomePage().setText(modifyContact.getHomePage());
			super.getB1().getEmail().setText(modifyContact.getEmail()); 
			super.getB1().getGroupe().setText(modifyContact.getGroupe()); 
			
			super.getForm().add(supprim);
			super.getForm().setVisible(true);
	}
}
	
	